import type { ElMessageBoxOptions } from 'element-plus'

function MessageBox() {
  let instance: ClMessageBox

  class ClMessageBox {
    #defaultMessageBoxOptions = {
      /** 打开 MessageBox 时是否自动获得焦点 */
      autofocus: false,
      /** MessageBox 的标题 */
      title: '信息',
      showCancelButton: true,
      /** 是否可通过按下 ESC 键关闭 MessageBox */
      closeOnPressEscape: false,
      /** 是否可通过点击遮罩层关闭 MessageBox */
      closeOnClickModal: false
    }
    #showMessageBox(options: ElMessageBoxOptions) {
      // ElMessageBox({
      //   ...this.#defaultMessageBoxOptions,
      //   ...options,
      // });
    }

    /** 自定义 */
    public custom(props: ElMessageBoxOptions) {
      this.#showMessageBox(props)
    }

    public delete(
      onConfirm: () => Promise<void>,
      onCatch?: () => Promise<void> | void,
      message?: string
    ) {
      this.#showMessageBox({
        message: message || '确定要删除吗',
        async beforeClose(action, instance, done) {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            try {
              await onConfirm()
              done()
            } catch {
              onCatch && onCatch()
              instance.confirmButtonLoading = false
            }
          } else {
            done()
          }
        }
      })
    }
  }
  return {
    getInstance() {
      return instance || (instance = new ClMessageBox())
    }
  }
}

export const clMsgBox = MessageBox().getInstance()
